{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "97651503",
   "metadata": {},
   "source": [
    "# week13_作业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "646ee723",
   "metadata": {},
   "source": [
    "## 1. 课堂练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8c057120",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "vowels: {'i', 'e', 'u', 'a', 'o'}\n",
      "Provide a word to search for vowels:hello world\n",
      "word: {' ', 'w', 'l', 'h', 'r', 'e', 'd', 'o'}\n",
      "o\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "print(\"vowels:\",vowels)\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "print(\"word:\",set(word))\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd08df2d",
   "metadata": {},
   "source": [
    "## 2. 课本第三章 课后项目练习（P143）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "46ea9745",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello world\n",
      "a was found 0 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 2 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] +=1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5ebf7ecb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello orla\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'e'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-9d5611610d26>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvowels\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m         \u001b[0mfound\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mletter\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msorted\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfound\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'e'"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] +=1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb42d779",
   "metadata": {},
   "source": [
    "###   ↑出错原因在于 没有初始化频度数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ce6150a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello orla\n",
      "a was found 1 time(s).\n",
      "e was found 1 time(s).\n",
      "o was found 2 time(s).\n"
     ]
    }
   ],
   "source": [
    "# 运用setdefault 方法，若没有为一个key 设置 value，setdefault 会赋他一个默认值\n",
    "\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter] +=1\n",
    "        \n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "560e87a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello world\n",
      "o\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bce330cf",
   "metadata": {},
   "source": [
    "## 3. 总结集合方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a413509",
   "metadata": {},
   "source": [
    "* 3.1 集合的性质\n",
    "\n",
    "> 3.1.1 集合是无序的\n",
    "\n",
    "> 3.1.2 集合是不重复的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5809d55b",
   "metadata": {},
   "source": [
    "* 3.2 集合的一般方法\n",
    "\n",
    "|序号|集合的方法|用法|使用结果|注意事项|\n",
    "|---|-----|-----|-----|-----|\n",
    "|1|增加|add()|若值已存在，则无法添加进列表|不会添加已有元素|\n",
    "|2|删除|pop();remove();discard|pop是随机删除;remove和discard是指定删除|remove一般与in一起使用;discard无法判断元素是否存在|\n",
    "|3|查询(in)|若存在，则显示为True,反之则False|||"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "101ae561",
   "metadata": {},
   "source": [
    "* 3.3 集合的数学逻辑运算\n",
    "\n",
    "|序号|运算名称|用法|逻辑运算符|注意事项|\n",
    "|---|-----|-----|-----|-----|\n",
    "|1|交集|set_1.intersection(set_2)|set_1 & set_2||\n",
    "|2|并集|set_1.union(set_2)|set_1并set_2||\n",
    "|3|差集|set_1.difference(set_2)|set_1.difference(set_2)|考虑先后顺序|\n",
    "|4|对称差集|set_1.symmetric_difference(set_2)|set_1.symmetric_difference(set_2)|两个差集合并|"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
